package com.atom.module.author.security;

import com.atom.module.author.pojo.entity.Author;
import com.atom.module.author.pojo.entity.Permission;
import com.atom.module.author.pojo.entity.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

@Getter
public abstract class LoginUser implements UserDetails {

    private final Author author;
    private List<Role> role;
    private List<Permission> permissions;

    public LoginUser(Author author) {
        this.author = author;
    }

    public abstract List<Permission> providerPermission(Author author);

    public abstract List<Role> providerRole(Author author);

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return getPermissions();
    }

    public List<Role> getRoles() {
        if (role == null) {
            role = providerRole(author);
        }
        return role;
    }

    public List<Permission> getPermissions() {
        if (permissions == null) {
            permissions = providerPermission(author);
        }
        return permissions;
    }

    public Long getId() {
        return this.author.getId();
    }

    @Override
    public String getPassword() {
        return author.getPassword();
    }

    @Override
    public String getUsername() {
        return author.getAccount();
    }

    /**
     * 账户是否未过期,过期无法验证
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     */
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     */
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
